$(
    function(){
        findById()
    }

)


layui.use(['form', 'layer', 'jquery', 'laydate'],function () {


    let laydate = layui.laydate;
    let $ = layui.jquery;
    let form = layui.form;
    let layer = layui.layer;

    laydate.render(
        {elem: '#L_birthday'}
    );
    form.verify(
        {
            username: [/^[\w-]{4,16}$/, "你输入的格式不正确"],
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
    form.on('submit(edit)', function (data) {
        data = data.field;
        data.hobby = getHobby().toLocaleString();
        data.id = sessionStorage.getItem("userId");
        data.loginName=sessionStorage.getItem("loginName")
        console.log(data)
        $.ajax({

            url: '/back/user/edit_info',
            type: 'post',
            dataType: 'json',
            data: data,
            success: function (res){
                console.log(res)
                if (res.result == "success") {
                    layer.alert("编辑成功", {
                            icon: 6
                        },
                        function () {
                            //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                }
                if(res.result=="notLogin"){
                    window.open("/html/login.html", "_top");
                }
                if (res.result == "repeat") {
                    layer.alert("用户名重复", {
                        icon: 1
                    });
                }
                if (res.result == "error"){
                    layer.alert("编辑失败", {
                        icon: 1
                    });
                }

            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                // 状态码
                console.log(XMLHttpRequest.status);
                // 状态
                console.log(XMLHttpRequest.readyState);
                // 错误信息
                console.log(textStatus);

            }
        })
        return false;
    });
});
function  findById() {

    let id = sessionStorage.getItem("userId")
    let loginName=sessionStorage.getItem("loginName")
    $.ajax({


        url: "/back/user/findInfo",
        data: {id:id,loginName:loginName},
        dataType: "json",
        type: "get",
        success:function (data) {
            if(data.result=="notLogin"){
                window.open("/html/login.html", "_top");
            }
            data=data.info;
            setUserData(data)

        }
    })

}

function setUserData(data){
    layui.use('form',function () {

        let form = layui.form;
        $("#L_username").val(data.username);
        $("#L_phone").val(data.mobile);
        $("#L_birthday").val(data.birthday);
        $("input[name=gender][value=" + data.gender + "]").attr("checked", true);

        if(data.hobby=='无'){

            $("input[name=hobby]").each(function () {

                $(this).prop("checked", false);

            })
        }

        let hobby = data.hobby.split(",");
        for (let j = 0; j < hobby.length; j++) {
            $("input[name=hobby]").each(function () {
                if (hobby[j] == $(this).val()) {
                    $(this).prop("checked", true);
                }
            })
        }
        form.render();
    })
}

function getHobby() {
    let value = [];

    $('input[name=hobby]:checked').each(function () {
        //这里用所选复选框或运算后的值
        value.push($(this).val());
    });
    if (value.length==0){
        value.push('无')
    }
    return value;
}
